<template>
  <div>
    <el-card style="width: 100%">
      <p class="pinformation">搜索条件</p>
      <el-form label-width="80" ref="queryRef" :model="queryParams">
        <el-row>
          <el-col :span="4">
            <el-form-item label="方案编号：" prop="projectCode">
              <el-input
                maxlength="100"
                clearable
                placeholder="请输入方案编号"
                v-model="queryParams.projectCode"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="方案名称：" prop="projectName">
              <el-input
                maxlength="100"
                clearable
                placeholder="请输入方案编号"
                v-model="queryParams.projectName"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="委托单位：" prop="wtCustomName">
              <el-input
                maxlength="100"
                clearable
                placeholder="请输入委托单位"
                v-model="queryParams.wtCustomName"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="开始时间：">
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="紧急类型：" prop="degree" label-width="80">
              <el-select
                v-model="queryParams.degree"
                placeholder="请选择紧急类型"
                clearable
                @change="handleQuery"
              >
                <el-option
                  v-for="dict in proxy.addListNoLimit(sys_degree)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-row style="justify-content: center">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="business resetBtn" @click="resetQuery"
                >重置</el-button
              >
            </el-row>
          </el-col>
        </el-row>
      </el-form>

      <el-row>
        <p class="pinformation">方案列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
      </el-row>
      <el-table
        ref="mainTableRef"
        :data="sampleWithList"
        @row-click="rowClick"
        v-loading="loading"
        border
      >
        <el-table-column
          label="方案编号"
          align="center"
          prop="projectCode"
          width="130"
        />
        <el-table-column
          label="方案名称"
          align="center"
          prop="projectName"
        />
        <el-table-column label="紧急类型" align="center" width="100" prop="degreeName"/>
        <el-table-column label="批次序号" align="center" prop="withNumber" width="120"/>
        <el-table-column label="采样人员" align="center" prop="cyUserName" width="120"/>
        <el-table-column label="采样时间" align="center" width="260">
          <template #default="scope">
            {{ scope.row.cyStartDate }}
            {{ scope.row.cyStartDate || scope.row.cyEndDate ? "-" : "" }}
            {{ scope.row.cyEndDate }}
          </template>
        </el-table-column>
        <el-table-column label="收样人" align="center" prop="getUserName" width="120"/>
        <el-table-column label="收样时间" align="center" prop="getDate" width="200"/>
        <el-table-column
          label="样品数量"
          align="center"
          prop="sampleCount"
          width="100"
        />
        <el-table-column label="状态" align="center" width="130" prop="stepName"/>
        <el-table-column label="操作" align="center" width="200">
          <template #default="scope">
            <el-button link type="success" @click="handleCreateWithTable(scope.row)">样品收样</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script setup name="SampleCollect">
import { getSampleWithList } from "@/api/sampleManage/sampleCollect";
const { proxy } = getCurrentInstance();
const router = useRouter();
const {  sys_degree, sys_sample_with_status } = proxy.useDict(
  "sys_degree",
  "sys_sample_with_status"
);
const {
  sys_contract_type,
} = proxy.useOrgDict(
  "sys_contract_type",
);
const queryParams = ref({ pageNum: 1, pageSize: 10 });
const dateRange = ref([]);
const loading = ref(false);
const sampleWithList = ref([]);
const total = ref(0);
function getList() {
  loading.value = true;
  queryParams.value.userStepId = 1;
  getSampleWithList(proxy.addDateRange(queryParams.value, dateRange.value)).then((res) => {
    sampleWithList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  getList();
}

function rowClick(params) {}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 74 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

function handleCreateWithTable(row) {
  router.push({
    path: "/sampleManage/withTable",
    query: { myId: row.myId },
  });
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});
</script>
